﻿using PasswdTool;
using PasswdTool.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using System.Text;

namespace Tables
{
    public class PasswdTable
    {
        private DataTable passwdTable;
        public PasswdTable()
        {
            passwdTable = new DataTable
            {
                TableName = "passwd"
            };
            var idColl = passwdTable.Columns.Add("id", typeof(int));
            idColl.AutoIncrement = true;
            idColl.AutoIncrementSeed = 1;
            idColl.AutoIncrementStep = 1;
            passwdTable.Columns.Add("user_id", typeof(int));
            passwdTable.Columns.Add("name", typeof(string));
            passwdTable.Columns.Add("username", typeof(string));
            passwdTable.Columns.Add("passwd", typeof(string));
            passwdTable.Columns.Add("add_time", typeof(DateTime));
            passwdTable.Columns.Add("update_time", typeof(DateTime));
        }

        public void Insert(PasswdModel model)
        {
            DataRow row = passwdTable.NewRow();
            row["user_id"] = model.UserId;
            row["name"] = model.Name;
            row["username"] = model.Username;
            row["passwd"] = model.Passwd;
            row["add_time"] = DateTime.Now;
            row["update_time"] = DateTime.Now;
            passwdTable.Rows.Add(row);
        }

        public int Remove(int id)
        {
            DataRow[] rows = passwdTable.Select("id="+id);
            int count = rows.Length;
            foreach (var item in rows)
            {
                item.Delete();
            }
            return count;
        }
        public int Update(PasswdModel model)
        {
            DataRow[] rows = passwdTable.Select("id=" + model.Id);
            int count = 0;
            for (int i = rows.Length - 1; i >= 0; i--)
            {
                bool isUpdate = false;
                if (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(rows[i]["mame"]))
                {
                    rows[i]["name"] = model.Name;
                    isUpdate = true;
                }
                if (!string.IsNullOrEmpty(model.Username) && model.Username.Equals(rows[i]["username"]))
                {
                    rows[i]["username"] = model.Username;
                    isUpdate = true;
                }
                if (!string.IsNullOrEmpty(model.Passwd) && model.Passwd.Equals(rows[i]["passwd"]))
                {
                    rows[i]["passwd"] = model.Passwd;
                    isUpdate = true;
                }
                if (isUpdate)
                {
                    rows[i]["update_time"] = DateTime.Now;
                    count++;
                }
            }
            return count;
        }
        public List<PasswdModel> Query(PasswdModel model)
        {
            List<PasswdModel> result = new List<PasswdModel>();
            StringBuilder query = new StringBuilder();
            if (model != null)
            {
                if (model.Id > 0)
                {
                    query.Append("id=" + model.Id);
                }
                if (model.UserId > 0)
                {
                    if (query.Length > 0)
                    {
                        query.Append(" and ");
                    }
                    query.Append("user_id=" + model.UserId);
                }
                if (!string.IsNullOrEmpty(model.Name))
                {
                    if (query.Length > 0)
                    {
                        query.Append(" and ");
                    }
                    query.Append("name= '" + model.Name + "'");
                }
                if (!string.IsNullOrEmpty(model.Username))
                {
                    if (query.Length > 0)
                    {
                        query.Append(" and ");
                    }
                    query.Append("username= '" + model.Username + "'");
                }
            }
            var rows = passwdTable.Select(query.ToString(),"id DESC");
            foreach (var row in rows)
            {
                result.Add(new PasswdModel()
                {
                    Id = int.Parse(row["id"].ToString()),
                    UserId = int.Parse(row["user_id"].ToString()),
                    Name = row["name"].ToString(),
                    Username = row["username"].ToString(),
                    Passwd = row["passwd"].ToString(),
                });  
            }
            return result;
        }
        public void Serialize()
        {
            GlobalContainer.Serialize(GlobalContainer.passwdTable, passwdTable);
        }

        public static PasswdTable Deserialize()
        {
            PasswdTable passwdTable = new PasswdTable
            {
                passwdTable = GlobalContainer.Deserialize(GlobalContainer.passwdTable)
            };
            return passwdTable;
        }
    }
}
